package gui;

import controllers.GUIController;
import hibernate.User;
import java.awt.*;
import java.awt.event.*;
import java.beans.PropertyVetoException;

import javax.swing.*;

@SuppressWarnings("serial")
public class ALogin extends JInternalFrame {

    private JButton loginButton;
    private JLabel logo;
    private JTextField usrField;
    private JLabel usrLabel;
    private JPanel userPassPanel;
    private JPanel bkgrd;
    private JButton addUserButton;
    private GUIController guiC;
    final ALogin obj = this;

    public ALogin(GUIController m) {
        guiC = m;
        generateLayout();
        addActionListener();
        addUserActionListener();
    }

    private void addActionListener() {  //Login Button
        final ALogin obj = this;
        loginButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                User u = new User();
                u.setCommonName(usrField.getText());
                
                if (loginValid(u)) {
                    obj.dispose();
                } else {
                    JOptionPane.showMessageDialog(null, "Invalid Login");
                }
            }
        });
    }

    //adds actionlistener to the add user button
    private void addUserActionListener() {

        addUserButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                obj.dispose();
                guiC.showGUI(new AAddUser(guiC, usrField));
            }
        });
    }

    private boolean loginValid(User u) {
        return guiC.processLogin(u);
    }

    private void generateLayout() {
        bkgrd = new javax.swing.JPanel();
        logo = new javax.swing.JLabel();
        userPassPanel = new javax.swing.JPanel();
        usrLabel = new javax.swing.JLabel();
        usrField = new javax.swing.JTextField();
        loginButton = new javax.swing.JButton();
        addUserButton = new javax.swing.JButton();

        bkgrd.setBackground(new java.awt.Color(0, 102, 255));

        logo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gui/OnPointlogo2-1.png"))); // NOI18N

        userPassPanel.setOpaque(false);

        usrLabel.setFont(new java.awt.Font("Tahoma", Font.BOLD, 12)); // NOI18N
        usrLabel.setText("Username: ");


        loginButton.setText("Login");
        loginButton.setOpaque(false);

        addUserButton.setText("Add User");
        addUserButton.setOpaque(false);

        javax.swing.GroupLayout userPassPanelLayout = new javax.swing.GroupLayout(userPassPanel);
       
        userPassPanel.setLayout(userPassPanelLayout);
        userPassPanelLayout.setHorizontalGroup(
               userPassPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                .addGroup(userPassPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(userPassPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                .addGroup(userPassPanelLayout.createSequentialGroup()
                .addComponent(usrLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(usrField, javax.swing.GroupLayout.PREFERRED_SIZE, 149, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(userPassPanelLayout.createSequentialGroup()
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED))
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, userPassPanelLayout.createSequentialGroup()
                .addComponent(addUserButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(loginButton, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));
        userPassPanelLayout.setVerticalGroup(
                userPassPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(userPassPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(userPassPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(usrLabel)
                .addComponent(usrField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(9, 9, 9) 
                .addGroup(userPassPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                
                )
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(userPassPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(loginButton)
                .addComponent(addUserButton))
                .addContainerGap(25, Short.MAX_VALUE)));

        javax.swing.GroupLayout bkgrdLayout = new javax.swing.GroupLayout(bkgrd);
        bkgrd.setLayout(bkgrdLayout);
        bkgrdLayout.setHorizontalGroup(
                bkgrdLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, bkgrdLayout.createSequentialGroup()
                //.addContainerGap(200, Short.MAX_VALUE)//changes made
                .addGroup(bkgrdLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                .addComponent(userPassPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(logo))
                .addGap(198, 198, 198)));
        bkgrdLayout.setVerticalGroup(
                bkgrdLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(bkgrdLayout.createSequentialGroup()
                .addGap(51, 51, 51)
                .addComponent(logo)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(userPassPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(115, Short.MAX_VALUE)));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(bkgrd, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE));
        layout.setVerticalGroup(
                //layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING.CENTER)
                .addComponent(bkgrd, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE));

        pack();
    }
}
